package com.itheima.project.details;

import com.itheima.project.basic.UserAuth;
import com.itheima.project.constant.SuperConstant;
import com.itheima.project.constant.security.OauthConstant;
import com.itheima.project.feign.CustomerAdapterFeign;
import com.itheima.project.feign.UserAdapterFeign;
import com.itheima.project.handler.LoginAuthHandler;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.utils.RegisterBeanHandler;
import com.itheima.project.vo.security.ResourceVo;
import com.itheima.project.vo.security.RoleVo;
import com.itheima.project.vo.security.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName ReactiveUserDetailsServiceImpl.java
 * @Description 支持flum的身份类实现ReactiveUserDetailsService接口
 */
@Slf4j
@Component("oauth2UserDetailsService")
public class Oauth2UserDetailsServiceImpl implements UserDetailsService {

    //调用RPC原创服务
    @Autowired
    UserAdapterFeign userAdapterFeign;

    //调用RPC原创服务
    @Autowired
    CustomerAdapterFeign customerAdapterFeign;

    @Autowired
    RegisterBeanHandler registerBeanHandler;

    @Override
    public UserDetails loadUserByUsername(String principal) throws UsernameNotFoundException {
        String[] principals = principal.split("#");
        if (principals.length!=4){
            log.warn("用户:{}登录信息不完整",principal);
            throw new DisabledException("用户:"+principal+"登录信息不完整");
        }
        String username = principals[0];
        Long enterpriseId = Long.valueOf(principals[1]);
        String clientId = principals[2];
        String loginType= principals[3];
        String loginTypeBeanName = OauthConstant.LoginAuthHandlers.get(loginType.split("-")[1]);
        LoginAuthHandler loginAuthHandler = registerBeanHandler.getBean(loginTypeBeanName,LoginAuthHandler.class);
        UserVo userVo = loginAuthHandler.findUserDetail(loginType,username,enterpriseId);
        //UserAuth构建
        if (EmptyUtil.isNullOrEmpty(userVo)) {
            throw new AccountExpiredException("该账号不存在!");
        } else if (SuperConstant.NO.equals(userVo.getEnableFlag())) {
            throw new DisabledException("该账户已被禁用!");
        }
        //资源
        List<ResourceVo> resourceVos = userAdapterFeign.findResourceByUserId(userVo.getId()).getDatas();
        if (!EmptyUtil.isNullOrEmpty(resourceVos)){
            Set<String> resources = resourceVos.stream().map(ResourceVo::getRequestPath).collect(Collectors.toSet());
            userVo.setResources(resources);
        }
        //角色
        List<RoleVo> reoleVos = userAdapterFeign.findRoleByUserId(userVo.getId()).getDatas();
        if (!EmptyUtil.isNullOrEmpty(reoleVos)){
            Set<String> roleLabel = reoleVos.stream().map(RoleVo::getLabel).collect(Collectors.toSet());
            userVo.setRoles(roleLabel);
        }
        //客户端
        userVo.setClientId(clientId);
        return new UserAuth(userVo);
    }
}
